<template>
  <el-dialog title="新增标签" :visible.sync="showDialog" width="30%" center>
    <!-- 表单内容 -->
    <el-form ref="form" label-width="120px" :rules="rules" :model="form">
      <el-form-item label="所属学科" prop="subjectName">
        <el-select
          v-model="form.subjectID"
          style="width: 290px"
          placeholder="请选择"
        >
          <el-option
            v-for="item in SubjectList"
            :key="item.id"
            :label="item.subjectName"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="目录名称" prop="directoryName">
        <el-input
          v-model="form.directoryName"
          style="width: 300px"
          size="mini"
        />
      </el-form-item>
      <el-form-item>
        <el-row type="flex" justify="center">
          <el-col :span="12">
            <el-button type="primary" size="mini" @click="submit"
              >确定</el-button
            >
            <el-button size="mini" @click="cancel">取消</el-button>
          </el-col>
        </el-row>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import { list } from '@/api/hmmm/subjects.js'
import { add, update } from '@/api/hmmm/directorys.js'
export default {
  props: {
    isUpdate: Boolean
  },
  data () {
    return {
      showDialog: false,
      rules: {
        directoryName: [
          { required: true, message: '目录名称不能为空', trigger: 'blur' }
        ]
      },
      SubjectList: [],
      form: {
        directoryName: '',
        subjectID: '',
        id: null
      }
    }
  },
  methods: {
    // SubjectList
    async getSubjectList () {
      const { data } = await list()
      this.SubjectList = [...data.items]
    },
    async submit () {
      if (this.isUpdate) {
        try {
          await this.$refs.form.validate()
          await update(this.form)
          this.$message.success('更新成功!')
          this.cancel()
          this.$emit('getSubjectList')
        } catch (error) {
          console.log(error)
        }
      } else {
        try {
          await this.$refs.form.validate()
          await add(this.form)
          this.$message.success('添加成功!')
          this.cancel()
          this.$emit('getSubjectList')
        } catch (error) {
          console.log(error)
        }
      }
    },
    cancel () {
      this.$refs.form.resetFields()
      this.showDialog = false
    }
  },
  async created () {
    this.getSubjectList()
  }
}
</script>

<style scoped lang="less"></style>
